﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is an own creation, 
    /// this just converts given pixels into a
    /// GetColorConstancy2 Formula and then puts the results
    /// back into pixel colors
    /// </summary>
    public class ColorConsistency : BaseImageFilter
    {
        protected double m_dAngle = 30.0f;

        /// <summary>
        /// default constructor
        /// </summary>
        public ColorConsistency()
        {
        }

        /// <summary>
        /// specify angle directly
        /// </summary>
        /// <param name="_dAngle"></param>
        public ColorConsistency(double _dAngle)
        {
            m_dAngle = _dAngle;
        }

        /// <summary>
        /// used to copy this instance
        /// </summary>
        /// <param name="_colorConsistency"></param>
        internal ColorConsistency(ColorConsistency _colorConsistency)
        {
            this.m_dAngle = _colorConsistency.m_dAngle;
        }

        /// <summary>
        /// gets or sets angle
        /// </summary>
        public double Angle
        {
            get { return m_dAngle; }
            set { m_dAngle = value; }
        }

        /// <summary>
        /// executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            int nValue = 0;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;
            double dConstancy = 0.0f;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    dConstancy = ColorFunctions.GetColorConstancy2(bitmap.GetPixel(x, y), Math.PI / 180 * m_dAngle);
                    if (Double.IsInfinity(dConstancy) 
                        || dConstancy == double.NaN 
                        || dConstancy == double.NegativeInfinity 
                        || dConstancy == double.PositiveInfinity)
                    {
                        continue;
                    }
                    else
                    {
                        if (dConstancy < 0)
                        {
                            dConstancy *= -1;
                            nValue = (int)(dConstancy * 100.0f);
                            if (nValue > 255) nValue = 255;
                            bitmap.SetPixel(x, y, Color.FromArgb(nValue, nValue, 0));
                        }
                        else
                        {
                            nValue = (int)(dConstancy * 100.0f);
                            if (nValue > 255) nValue = 255;
                            bitmap.SetPixel(x, y, Color.FromArgb(0, nValue, nValue));
                        }
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ColorConsistency(this);
        }
    }
}
